;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; This file is part of ICCLE2.
;
; ICCLE2 is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; ICCLE2 is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with ICCLE2.  If not, see <http://www.gnu.org/licenses/>.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun numeric2Discrete (n)
  (let ((d (make-discrete :name    (numeric-name    n)
                          :classp  (numeric-classp  n)
                          :ignorep (numeric-ignorep n)
                          :pos     (numeric-pos     n))))
    (dolist (one (numeric-all n))
      (sniff d one))
    (taste d)
    d))

(defun discretize-rows (headers rows 4class 4others)
  (mapcar #'(lambda (row)
	      (discretize-row headers row 4class 4others))
	  rows))

(defun discretize-row (headers row 4class 4others)
  (mapcar #'(lambda (header row)
	      (discretize-cell header cell 4class 4others))
	  headers row))

(defmethod discretize-cell ((n numeric) x 4class 4others)
  (if (header-classp n)
      (funcall 4class  n x)
      (funcall 4others n x)))

(defmethod discretize-cell ((d discrete) x f1 f2)
  (declare (ignore f1 f2))
  (sym->num d x))
  
(defun sym->num  ((d discrete) sym)
  (cdr (assoc sym (discrete-ranks d))))

(defun num->sym  (d num)
  (car (rassoc num (discrete-ranks d))))

(defun num->ewd (n num)
  (floor
   (/ (- num (numeric-min n))
      (/ (- (numeric-max n) (numeric-min n))
	 (numeric-bins n)))))
      
(defun ewd->num (n ewd)
  (let ((width (- (numeric-max n) (numeric-min n))))
    (+ (numerc-min n)
       (* width (+ 0.5 ewd)))))

(defun num->bore (n num)
   (if (< (numeric-best n) 0)
       (if (<= num (numeric-best-num n)) 1 0)
       (if (>= num (numeric-best-num n)) 1 0)))
 
(defun bore->num (n bore)
  (let ((num1 (* 0.5 (- (numeric-best n) (numeric-min n))))
	(num2 (* 0.5 (- (numeric-max  n) (numeric-best n)))))
    (if (< (numeric-best n) 0)
	(if (zerop n) num2 num1)
	(if (zerop n) num1 num2))))	  
